package com.example.compose

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import com.example.compose.viewmodel.ViewModel
import com.example.repository.model.CartoonInfo
import com.google.accompanist.coil.rememberCoilPainter
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    private val viewModel: ViewModel by viewModels()

    @ExperimentalFoundationApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.getHomeCartoon()
        viewModel.msg3LiveData.observe(this) {
            Log.i("TAG", "onCreate: ${it.getString("img")}")
            val intent = Intent(this, DetailActivity::class.java)
            intent.putExtras(it)
            startActivity(intent)
        }
        viewModel.homeLiveData.observe(this) {
            setContent {
                Greeting(viewModel)
            }
        }
    }


}


@ExperimentalFoundationApi
@Composable
fun Greeting(viewModel: ViewModel) {
    MaterialTheme {
//        val typography = MaterialTheme.typography
        LazyVerticalGrid(cells = GridCells.Fixed(2)) {
            items(viewModel.cartoonInfors) { data ->
                //Column函数可让您垂直堆叠元素
                Card(modifier = Modifier
                    .padding(4.dp)
                    .height(200.dp)
                    .width(112.dp)
//            .clip(shape = RoundedCornerShape(5.dp))
                    .clickable {
                        viewModel.getHomeCartoon(data)
                    }) {
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally,
                    ) {
                        Image(
                            painter = rememberCoilPainter(
                                request = data.img
                            ),
                            contentDescription = "image",
                            contentScale = ContentScale.Crop,
                            modifier = Modifier
                                .height(150.dp)
                        )
                        Text(text = data.title, maxLines = 1)
                        Text(text = "时间：${data.type}", fontSize = 10.sp, maxLines = 1)
                    }
                }
            }
        }
    }
}


//@Composable
//fun Greeting(name: String, time: Long) {
//    val viewModel: ViewModel = viewModel()
//    MaterialTheme {
//        val typography = MaterialTheme.typography
//        Row(
//            verticalAlignment = Alignment.CenterVertically,
//            modifier = Modifier.padding(10.dp)
//        ) {
//            Image(
//                painter = rememberCoilPainter(request = R.drawable.lx),
//                contentDescription = "image",
//                contentScale = ContentScale.Crop,
//                modifier = Modifier
//                    .width(300.dp)
//                    .height(300.dp)
//                    .clip(shape = RoundedCornerShape(5.dp))
//                    .clickable { viewModel.detailLiveData.postValue(true) }
//            )
//            //Column函数可让您垂直堆叠元素
//            Column(modifier = Modifier.padding(start = 10.dp)) {
//                Text(text = name, style = typography.h6)
//                Text(text = "时间：$time", style = typography.body2)
//            }
//        }
//    }
//}

//@Preview(showBackground = true)
//@Composable
//fun DefaultPreview() {
//    ComposeTheme {
//        Greeting("Android", 1111111)
//    }
//}